package com.heima.wemedia.controller;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.wemedia.dtos.WmMaterialDto;
import com.heima.model.wemedia.pojos.WmMaterial;
import com.heima.wemedia.service.WmMaterialService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.UnsupportedEncodingException;
import java.util.List;
import java.util.concurrent.CompletableFuture;

@RestController
@RequestMapping("/api/v1/material")
public class WmMaterialController {
    @Autowired
    private WmMaterialService wmMaterialService;

    /**
     * 图片/素材上传
     * @param multipartFile 图片/素材信息
     */
    @PostMapping("/upload_picture")
    public ResponseResult<WmMaterial> uploadPicture(MultipartFile multipartFile) {
        return wmMaterialService.uploadPicture(multipartFile);
    }

    /**
     * 查询图片/素材列表
     * @param dto 查询参数
     */
    @PostMapping("/list")
    public ResponseResult<List<WmMaterial>> findList(@RequestBody WmMaterialDto dto){
        return wmMaterialService.findList(dto);
    }

    /**
     * 删除图片
     * @param imgId 图片id
     */
    @GetMapping("/del_picture/{imgId}")
    public ResponseResult<String> delPicture(@PathVariable Integer imgId)
            throws JsonProcessingException, UnsupportedEncodingException {
        return wmMaterialService.delPicture(imgId);
    }

    /**
     * 收藏图片
     * @param id 图片id
     */
    @GetMapping("/collect/{id}")
    public ResponseResult<String> collect(@PathVariable Integer id) {
        return wmMaterialService.collect(id);
    }

    /**
     * 取消收藏
     * @param id 图片id
     */
    @GetMapping("/cancel_collect/{id}")
    public ResponseResult<String> cancelCollect(@PathVariable Integer id) {
        return wmMaterialService.cancelCollect(id);
    }
}
